package com.uenpay.im.ui.old.activity.nearby;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.qyx.android.message.type.PacketType;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.QYXUserEntity;
import com.uenpay.im.weight.RoundImageView;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2015-12-12 15:45:42
 * @copyRight http://www.qiyunxin.com
 * @function TODO
 */
public class NearbyPeopleAdapter extends BaseAdapter {

	private LayoutInflater inflater;
	private ArrayList<QYXUserEntity> arrayList;

	public NearbyPeopleAdapter(Context _Context,
			ArrayList<QYXUserEntity> _ArrayList) {
		inflater = LayoutInflater.from(_Context);
		arrayList = _ArrayList;
	}

	@Override
	public int getCount() {
		return arrayList == null ? 0 : arrayList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return arrayList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		NearbyViewHolder holder = null;
		QYXUserEntity qyxUserEntity = arrayList.get(arg0);
		if (arg1 == null) {
			arg1 = inflater.inflate(R.layout.item_nearby_layout, arg2,false);
			holder = new NearbyViewHolder();
			holder.name_tv = (TextView) arg1.findViewById(R.id.name_tv);
			holder.geodistance = (TextView) arg1.findViewById(R.id.geodistance);
			holder.signature_tv = (TextView) arg1
					.findViewById(R.id.signature_tv);
			holder.avatar_iv = (RoundImageView) arg1
					.findViewById(R.id.avatar_iv);
			holder.sex_iv = (ImageView) arg1.findViewById(R.id.sex_iv);
			arg1.setTag(holder);
		} else {
			holder = (NearbyViewHolder) arg1.getTag();
		}
		String name = IMApplication.config.getFriendsRemarkName(qyxUserEntity.custid);
		if(TextUtils.isEmpty(name)){
			holder.name_tv.setText(qyxUserEntity.nickname);
		}else{
			holder.name_tv.setText(name);
		}

		holder.geodistance.setText(qyxUserEntity.idenum);
		if (!TextUtils.isEmpty(qyxUserEntity.realname)) {
			holder.signature_tv.setText(qyxUserEntity.realname);
			holder.signature_tv.setVisibility(View.VISIBLE);
		} else {
			holder.signature_tv.setVisibility(View.GONE);
		}

		if(qyxUserEntity.sex == 1){
			holder.sex_iv.setImageResource(R.drawable.icon_man);
		}else{
			holder.sex_iv.setImageResource(R.drawable.icon_woman);
		}
		holder.avatar_iv.SetUrl(APIConfiguration
				.getAvatarUrlNormal(qyxUserEntity.custid, PacketType.PERSONAL));
		return arg1;
	}

	private class NearbyViewHolder {
		private TextView name_tv, geodistance, signature_tv;
		private RoundImageView avatar_iv;
		private ImageView sex_iv;
	}

}
